package com.yunji.base.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunji.base.bo.BelongAmountModifyBO;
import com.yunji.base.bo.BelongSelectQueryBO;
import com.yunji.base.dao.IBelongDao;
import com.yunji.base.domain.BelongDO;
import com.yunji.base.mapper.IBelongMapper;
import com.yunji.base.params.BelongPageParam;
import com.yunji.base.params.BelongSearchParam;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.framework.MybatisPlusHelper;
import com.yunji.common.utils.StringUtils;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.user.dao.impl
 * @Title: BelongDaoImpl
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-23 16:53:02      V1.0        HuaAo       新建类
 */
@Repository
public class BelongDaoImpl extends BaseDaoImpl<IBelongMapper, BelongDO> implements IBelongDao {


    @Override
    public Integer selectStockStatus(Long belongId) {
        LambdaQueryWrapper<BelongDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BelongDO::getStockStatus).eq(BelongDO::getBelongId, belongId);
        BelongDO belong = baseMapper.selectOne(wrapper);
        return BeanUtils.isNotNull(belong) ? belong.getStockStatus() : LogicCode.NO;
    }

    @Override
    public List<BelongDO> listByAgentIds(String roleType, List<Long> agentIds) {
        return lambdaQuery().in(BelongDO::getAgentId, agentIds).eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).list();
    }

    @Override
    public List<BelongDO> listByAgentIds(String roleType, Long agentId) {
        return lambdaQuery().eq(BelongDO::getAgentId, agentId).eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).list();
    }

    @Override
    public List<BelongDO> listByPid(String roleType, Long pid) {
        return lambdaQuery().eq(BelongDO::getParentId, pid)
                .eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).list();
    }

    @Override
    public List<BelongDO> listById(String roleType, Long belongId) {
        return lambdaQuery().eq(BelongDO::getBelongId, belongId)
                .eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).list();
    }

    @Override
    public BelongDO getBySiteId(String roleType, Long siteId) {
        return lambdaQuery().eq(BelongDO::getSiteId, siteId)
                .eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).one();
    }

    @Override
    public BelongDO getByAgentId(String roleType, Long agentId) {
        return lambdaQuery().eq(BelongDO::getAgentId, agentId)
                .eq(StringUtils.isNotEmpty(roleType), BelongDO::getBelongType, roleType).one();
    }

    @Override
    public List<Long> getSiteIdsById(Long pid) {
        return baseMapper.selectObjs(new LambdaQueryWrapper<BelongDO>().select(BelongDO::getSiteId).eq(BelongDO::getParentId, pid)
                        .eq(BelongDO::getBelongType, UserType.SITE.getCode())).stream()
                // 指定要转换的类型
                .map(o -> (Long) o)
                .collect(Collectors.toList());
    }

    @Override
    public List<BelongDO> getByBelongType(String belongType) {
        return lambdaQuery().eq(BelongDO::getBelongType, belongType).list();
    }

    @Override
    public List<BelongDO> search(BelongPageParam belong) {
        return pageList(belong);
    }

    @Override
    public List<BelongDO> getByScaleType(Integer scaleType) {
        return lambdaQuery().eq(BelongDO::getScaleType, scaleType).list();
    }

    @Override
    public List<String> searchTableField(BelongSearchParam param) {
        return this.baseMapper.searchTableField(param);
    }


    //    <update id="updateAccount">
//    UPDATE
//            v3_belong
//    SET
//            <if test="earnAmount != null"> 		earn_amount 		= 	earn_amount 	+ #{earnAmount}, 	</if>
//            <if test="cashAmount != null"> 		cash_amount 		= 	cash_amount 	+ #{cashAmount}, 	</if>
//            <if test="remainAmount != null"> 	remain_amount 		= 	remain_amount 	+ #{remainAmount}, 	</if>
//            <if test="frozenAmount != null"> 	frozen_amount 		= 	frozen_amount 	+ #{frozenAmount}, 	</if>
//            <if test="refundAmount != null"> 	refund_amount 		= 	refund_amount 	+ #{refundAmount}, 	</if>
//    updater 			= 	#{updater},
//    update_name 		= 	#{updateName},
//    update_time 		= 	#{updateTime}
//    WHERE
//            del_flag = 0 AND belong_id = #{belongId}
//    </update>
    @Override
    public boolean updateAccount(BelongAmountModifyBO belong) {
        return lambdaUpdate()
                .setSql(BeanUtils.isNotNull(belong.getEarnAmount()), MybatisPlusHelper.columnAddCount(BelongDO::getEarnAmount, belong.getEarnAmount()))
                .setSql(BeanUtils.isNotNull(belong.getCashAmount()), MybatisPlusHelper.columnAddCount(BelongDO::getCashAmount, belong.getCashAmount()))
                .setSql(BeanUtils.isNotNull(belong.getRemainAmount()), MybatisPlusHelper.columnAddCount(BelongDO::getRemainAmount, belong.getRemainAmount()))
                .setSql(BeanUtils.isNotNull(belong.getFrozenAmount()), MybatisPlusHelper.columnAddCount(BelongDO::getFrozenAmount, belong.getFrozenAmount()))
                .setSql(BeanUtils.isNotNull(belong.getRefundAmount()), MybatisPlusHelper.columnAddCount(BelongDO::getRefundAmount, belong.getRefundAmount()))
                .eq(BelongDO::getBelongId, belong.getBelongId()).update();
    }

    @Override
    public BelongDO select(BelongSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<BelongDO>()
                .eq(BeanUtils.isNotNull(bo.getSiteId()), BelongDO::getSiteId, bo.getSiteId())
                .eq(BeanUtils.isNotNull(bo.getAgentId()), BelongDO::getAgentId, bo.getAgentId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()), BelongDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getBelongType()), BelongDO::getBelongType, bo.getBelongType()));
    }
}